﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;
using System.Threading;

public partial class Login : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["LoginID"] != null)
            {
                Response.Redirect("~/Default.aspx");
            }
        }
    }
    protected void lbtnTW_Click(object sender, EventArgs e)
    {
        HttpCookie hc = new HttpCookie("Language", "zh-hk");
        Response.SetCookie(hc);
        Response.Redirect(Request.Url.ToString());
    }
    protected void lbtnCHN_Click(object sender, EventArgs e)
    {
        HttpCookie hc = new HttpCookie("Language", "zh-cn");
        Response.SetCookie(hc);
        Response.Redirect(Request.Url.ToString());
    }
    protected void lbtnEnglish_Click(object sender, EventArgs e)
    {
        HttpCookie hc = new HttpCookie("Language", "en");
        Response.SetCookie(hc);
        Response.Redirect(Request.Url.ToString());
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        bool isLoginValid = false;
        if (Session["LoginValid"] != null)
        {
            if (Session["LoginValid"].ToString().Trim() == txtValidCode.Text.Trim())
            {
                isLoginValid = true;
            }
        }

        txtValidCode.Text = string.Empty;

        if (!isLoginValid)
        {
            Jscript.Alert(Resources.Messages.ValidWrong, this.Page);
            return;
        }

        IRolePurviewHandler userManage = new RolePurviewHandler();
        string password = StringUtil.EncryptPassword(txtPassword.Text.Trim(), "MD5");
        UsersQuery query = userManage.UserLogin(txtLoginName.Text.Trim(), password);
        if (query != null)
        {
            Session["LoginID"] = query.LoginID;
            Session["UserID"] = query.UserID.ToString();
            Session["LoginUserClientID"] = query.ClientID.ToString();
            Session["UserName"] = query.UserName;
            if (Request["returnUrl"] != null)
            {
                Response.Redirect(Request["returnUrl"]);
            }
            else
            {
                Response.Redirect("~/UserManage/Default.aspx");
            }
        }
        else
        {

            Jscript.Alert(Resources.Messages.LoginError, this.Page);


            txtPassword.Focus();
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtLoginName.Text = string.Empty;
        txtPassword.Text = string.Empty;
    }
}
